package org.example.library_management_system.Controller;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.library_management_system.Model.Result;
import org.example.library_management_system.Model.UserInfo;
import org.example.library_management_system.Service.UserService;
import org.example.library_management_system.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/Login",produces = "application/json")
    public Result Login(UserInfo user, HttpSession session){
        log.info("接收到的数据信息:{}",user);

        if(!StringUtils.hasLength(user.getUserName())||!StringUtils.hasLength(user.getPassword())){
//            return "账号,密码不能为空";
            return Result.fail("账号,密码不能为空");

        }


//        if(!"123".equals(user.getUserName())||!"123".equals(user.getPassword())){
//            return "密码错误";
//        }

        String password = userService.selectByUserName(user.getUserName());
        if(password==null || !password.equals(user.getPassword())){
//            return "密码错误";
            return Result.fail("密码错误");
        }else {
            session.setAttribute(Constants.USER_NAME,user);
//            return "欢迎登录";
            return Result.success("欢迎登录");
        }


    }
}
